<?php
	class Request{
		const TGET = INPUT_GET;
		const TPOST = INPUT_POST;
		const TCOOKIE = INPUT_COOKIE;
		const TFILE = INPUT_POST;
		const TSERVER = INPUT_SERVER;
		const TREQUEST = 9;

		private $_get = array();
		private $_post =  array();
		private $_cookie = array();
		private $_files = array();
		private $_server = array();
		private $_request = array();

		private $_exception = array(
				"REQUEST_TIME_FLOAT",
				"REQUEST_TIME"
			);
		private $_match = array(
				FILTER_VALIDATE_INT,
				FILTER_VALIDATE_FLOAT,
				FILTER_VALIDATE_BOOLEAN,
				FILTER_SANITIZE_SPECIAL_CHARS
			);


		public function __construct(){
			$this->_get = $this->verify($_GET,self::TGET);
			$this->_post = $this->verify($_POST,self::TPOST);
			$this->_cookie = $this->verify($_COOKIE,self::TCOOKIE);
			$this->_files = $this->verify($_FILES,self::TFILE);
			$this->_server = $this->verify($_SERVER,self::TSERVER);
			$this->_request = $this->verify($_REQUEST,self::TREQUEST);
		}

		private function verify($data,$type=false){
			if(is_array($data)){
				foreach ($data as $key => $value) {
					unset($data[$key]);
					switch ($type) {
							case INPUT_GET:
							case INPUT_POST:
							case INPUT_SERVER:
							case INPUT_COOKIE:
								if(filter_has_var($type, $key)){
									$data[$this->verify($key)] = $this->verify($value);
								}else{
									//php5.4 jia ru filter hanshu rufa yanzheng
									if(in_array($key, $this->_exception)){
										$data[$this->verify($key)] = $this->_match($value);
									}
								}
								break;
								
							case self::TREQUEST:
								if(filter_has_var(INPUT_GET,$key) || filter_has_var(INPUT_POST, $key)){
									$data[$this->verify($key)] = $this->verify($value);
								}
								break;
							default :
								$data[$this->verify($key)] = $this->verify($value);
								break;
					}
				}
			}else{
				$data = $this->_match($data);
			}
			return $data;
		}

		private function _match($value,$offset = 0){
			if($offset<count($this->_match)){
				return filter_var($value,$this->_match[$offset])!==false
							 ? filter_var($value,$this->_match[$offset]) : $this->_match($value,++$offset);
			}else{
				return filter_var($value,$this->_match[3]);
			}
		}
		public function getPost($name = 'all',$value=false,$isverify = true){
			
			if(strtolower($name) != 'all'){
				$name = (string)$name;
				if(array_key_exists($name, $this->_post)){
					
					if($isverify !==true){
						return $_POST[$name];
					}
					return $this->_post[$name];
				}else{
					return $value;
				}
			}else{
				if($isverify !==true){
						return empty($_POST) ? $value : $_POST;
				}
				return empty($this->_post) ? $value : $this->_post;
			}
		}

		public function getCookie($name,$value=false,$isverify = true){
			$name = (string)$name;
			if(array_key_exists($name, $this->_cookie)){
				if($isverify !==true){
					return $_COOKIE[$name];
				}
				return $this->_cookie[$name];
			}else{
				return $value;
			}
		}

		public function getServer($name,$value=false,$isverify = true){
			$name = (string)$name;
			if(array_key_exists($name, $this->_server)){
				if($isverify !==true){
					return $_SERVER[$name];
				}
				return $this->_server[$name];
			}else{
				return $value;
			}
		}

		public function getFiles($name,$value=false,$isverify = true){
			$name = (string)$name;
			if(array_key_exists($name, $this->_files)){
				if($isverify !==true){
					return $_FILES[$name];
				}
				return $this->_files[$name];
			}else{
				return $value;
			}
		}

		public function getGet($name = 'all',$value=false,$isverify = true){
			if(strtolower($name) != 'all'){
				$name = (string)$name;
				if(array_key_exists($name,$this->_get)){
					if($isverify !==true){
						return $_GET[$name];
					}
					return $this->_get[$name];
				}else{
					return $value;
				}
			}else{
				if($isverify !==true){
					return empty($_GET) ? $value : $_GET;
				}
				return empty($this->_get) ? $value : $this->_get;
			}
		}

		public function getRequest($name = 'all',$value=false,$isverify = true){
			if(strtolower($name) != 'all'){
				$name = (string)$name;
				if(array_key_exists($name, $this->_request)){
					if($isverify !==true){
						return $_REQUEST[$name];
					}
					return $this->_request[$name];
				}else{
					return $value;
				}
			}else{
				if($isverify !==true){
					return empty($_REQUEST) ? $value : $_REQUEST;
				}
				return empty($this->_request) ? $value : $this->_request;
			}
		}
	}
?>